﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfAppQuestion01.Base;

public class MvvmCommand:ICommand
{
    public event EventHandler? CanExecuteChanged;

    public Action<object?>? DoExecuteAction;
    public Func<bool>? CanExecuteAction = null;


    public MvvmCommand(Action<object?> _DoExecuteAction)
    {
        DoExecuteAction = _DoExecuteAction;
    }

    public MvvmCommand(Action<object?> _DoExecuteAction, Func<bool>? canExecuteAction)
    {
        DoExecuteAction = _DoExecuteAction;
        CanExecuteAction = canExecuteAction;
    }

    public bool CanExecute(object? parameter)
    {
        return CanExecuteAction?.Invoke() ?? true;
    }


    public void Execute(object? parameter)
    {
        DoExecuteAction?.Invoke(parameter);
    }

    public void ResetCanExecute()
    {
        CanExecuteChanged?.Invoke(this, EventArgs.Empty);
    }
}
